package com.project.weimi.adapter;

import java.util.List;

import com.project.weimi.R;
import com.project.weimi.entity.response.TaoTaoEntity;
import com.project.weimi.view.RoundImageView;
import com.project.weimi.widget.ToastMessage;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class TaoTaoAdapter extends BaseAdapter {

	private Context context;
	private List<TaoTaoEntity> taoList;

	public TaoTaoAdapter(Context context, List<TaoTaoEntity> circleList) {
		super();
		this.context = context;
		this.taoList = circleList;
	}

	@Override
	public int getCount() {
		return taoList.size();
	}

	@Override
	public Object getItem(int position) {
		return taoList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder vh = null;
		if (convertView == null) {
			vh = new ViewHolder();
			// 解析布局文件
			convertView = LayoutInflater.from(context).inflate(R.layout.fragment_home_tao_item, null);
			vh.tao_title = (TextView) convertView.findViewById(R.id.tao_title);
			vh.tao_more = (FrameLayout) convertView.findViewById(R.id.tao_more);
			vh.vp1 = (ViewPager) convertView.findViewById(R.id.vp1);
			vh.view7 = convertView.findViewById(R.id.view7);
			vh.view8 = convertView.findViewById(R.id.view8);
			vh.tao_tv1 = (TextView) convertView.findViewById(R.id.tao_tv1);
			vh.tao_iv1 = (ImageView) convertView.findViewById(R.id.tao_iv1);
			vh.tao_tv2 = (TextView) convertView.findViewById(R.id.tao_tv2);
			vh.tao_iv2 = (ImageView) convertView.findViewById(R.id.tao_iv2);

			vh.vp1.setLayoutParams(new FrameLayout.LayoutParams(context.getResources().getDisplayMetrics().widthPixels,
					context.getResources().getDisplayMetrics().widthPixels * 188 / 563));

			convertView.setTag(vh);
		} else {
			vh = (ViewHolder) convertView.getTag();
		}

		final TaoTaoEntity taoEntity = (TaoTaoEntity) getItem(position);

		// ViewPager适配器
		HomeFragmentViewPagerAdapter adapter = new HomeFragmentViewPagerAdapter(context, taoEntity.getImageView());
		vh.vp1.setAdapter(adapter);
		vh.vp1.setCurrentItem(100 * 1000);
		// Viewpager滑动监听
		vh.vp1.addOnPageChangeListener(new TaoOnPageChangeListener(taoEntity.getImageView().size(), vh.view7, vh.view8));
		vh.vp1.setCurrentItem(0);

		vh.tao_title.setText(taoEntity.getTao_title());
		vh.tao_more.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				ToastMessage.showToast(context, "WeiMi:" + taoEntity.getTao_title());
			}
		});

		vh.tao_tv1.setText(taoEntity.getTao_tv()[0]);
		vh.tao_iv1.setImageResource(taoEntity.getTao_iv()[0]);
		vh.tao_tv2.setText(taoEntity.getTao_tv()[1]);
		vh.tao_iv2.setImageResource(taoEntity.getTao_iv()[1]);

		return convertView;
	}

	class TaoOnPageChangeListener implements OnPageChangeListener {
		int taoListSize;
		View view1;
		View view2;

		public TaoOnPageChangeListener(int taoListSize, View view1, View view2) {
			super();
			this.taoListSize = taoListSize;
			this.view1 = view1;
			this.view2 = view2;
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub

		}

		@Override
		public void onPageSelected(int arg0) {
			// TODO Auto-generated method stub
			view1.setBackgroundResource(R.drawable.shape_trans_circle);
			view2.setBackgroundResource(R.drawable.shape_trans_circle);

			switch (arg0 % (taoListSize / 2)) {
			case 0:
				view1.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			case 1:
				view2.setBackgroundResource(R.drawable.shape_white_circle);
				break;
			default:
				break;
			}
		}
	}

	class ViewHolder {
		TextView tao_title;
		FrameLayout tao_more;
		ViewPager vp1;
		View view7;
		View view8;
		TextView tao_tv1;
		ImageView tao_iv1;
		TextView tao_tv2;
		ImageView tao_iv2;
	}

}
